package com.accenture.portal.controller.role;

import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;

import com.accenture.portal.model.access.Access;
import com.accenture.portal.model.role.Role;
import com.accenture.portal.service.role.RoleService;


@Controller
public class RoleController {
	
	@Autowired
	private RoleService roleService;

	
	/**
	 * 显示角色列表
	 * @param map
	 * @return
	 */
	@RequestMapping("role.action")
	public String getAllRolelist(HttpServletRequest request, HttpServletResponse response,
			Model model){
		List<Role> roleList = roleService.listAllRoles();
		model.addAttribute("roleList", roleList);
//		map.put("roleList", roleList);
		return "Role/role";
	}
	
	/**跳转添加页面
	 * @param request
	 * @param response
	 * @param model
	 * @return 
	 * @return
	 */
	@RequestMapping(value ="addRolePage.action", method = RequestMethod.GET)
	public String addRole(HttpServletRequest request, HttpServletResponse response,Model model, Access access) {
		List<Access> list = roleService.allAccess();
		model.addAttribute("accessList", list);
		return "Role/addRole";
	}
	/**返回角色显示页面
	 * @param request
	 * @param response
	 * @param model
	 * @return
	 */
	@RequestMapping(value="addRole.action", method=RequestMethod.POST)
	public String addRole(@ModelAttribute("role") Role role,HttpServletRequest request,
			HttpServletResponse response, Model model) {
		roleService.insertRole(role);
		int number = (int)request.getSession().getAttribute("count");
		return "redirect:Page.action?number=" + number;
	}

	/**
	 * logical Delete Role
	 * 
	 * @param request
	 * @param response
	 * @param accessId
	 * @return
	 * @throws Exception
	 */
	@RequestMapping(value = "logicalDeleteRole.action", method = RequestMethod.GET)
	public String logicalDeleteRole(@RequestParam("roleId") Integer roleId,
			HttpServletRequest request, HttpServletResponse response,
			Model model) {
		roleService.logicalDeleteRole(roleId);
		int number = (int)request.getSession().getAttribute("number");
		return "redirect:Page.action?number=" + number;
	}
	/*
	 * 跳转到editRole.jsp
	 * 
	 * */
	  @RequestMapping(value ="updateRolePage.action", method = RequestMethod.GET)  
	    public String updateRole(HttpServletRequest request, HttpServletResponse response,Integer roleId,Model model){
		  Role role = roleService.getRoleById(roleId);
		  model.addAttribute("role",role);
		  List<Access> list = roleService.allAccess();
	      model.addAttribute("accessList", list);
	      return "Role/editRole";
	    }
		/*
		 * editRole.jsp保存
		 * */
		@RequestMapping(value = "updateRole.action", method = RequestMethod.POST)
		public String updateRole(@ModelAttribute("role") Role role,HttpServletRequest request, HttpServletResponse response,Model model) {
			roleService.updateRoleBaseInfo(role);
			int number = (int)request.getSession().getAttribute("number");
			return "redirect:Page.action?number=" + number;
		}
	 /**  
     *分页 
     * @param number  
     * @param request  
     * @return  
     */  
	   @RequestMapping(value="Page.action")  
	    public String Page(@RequestParam("number") int number, HttpServletRequest request,Model model){
			
	    	// 设置每页显示多少行
	    	int page = 6;
	    	
			//获取当前页
			if (number < 1) {
				number = 1;
			}
			
			//总条数
	    	int total = roleService.totalCount();
	    	//总页数
	    	int count = total/page;
	    	if (total % page != 0) {
	    		count++;
	    	}
	    	if(count==0){
	    		count=1;
	    	}
	    	if (number > count) {
	    		number = count;
	    	}
	    	
	    	List<Role> list = roleService.Page((number-1)*page, page);
	    	
	    	request.getSession().setAttribute("number", number);
	    	model.addAttribute("roleList", list);
	    	request.getSession().setAttribute("count", count);
	    	
	    	return "Role/role";
	    }
}


